package com.ruoyi.system.privilege;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.DataPermissionRule;
import com.ruoyi.system.mapper.DataPermissionRuleMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 初始化数据权限规则
 *
 * @author dbq
 * @date 2021/9/9 17:50
 */
@Component
public class DataPermissionRuleInit {
    final DataPermissionRuleMapper dataPermissionRuleMapper;

    final RedisCache redisCache;

    public DataPermissionRuleInit(DataPermissionRuleMapper dataPermissionRuleMapper, RedisCache redisCache) {
        this.dataPermissionRuleMapper = dataPermissionRuleMapper;
        this.redisCache = redisCache;
    }

    @PostConstruct
    public void init() {
        List<DataPermissionRule> privilegeList = dataPermissionRuleMapper.selectList(new QueryWrapper<>());
        redisCache.deleteObject("data_privilege_rule:");
        redisCache.setCacheObject("data_privilege_rule:", JSONObject.toJSONString(privilegeList));
    }
}
